<?php

namespace app\admin\controller;

use think\Db;

use app\common\service\SysLogService;

class Dictionary extends Common
{

    function getAllDictionaryType()
    {
        $map = array();
        $map['status'] = 1;
        $map['is_delete'] = 0;
        $result = Db::table("zstp_dictionary")->field("dictionary_type as  label  ,  dictionary_type as  `value` , id as `key` ")->where($map)->group("dictionary_type")->select();
        return json(array("code" => 200, "data" => $result, "message" => "获取成功"));

    }


    function getDictionaryById()
    {
        $data = request()->put();


        $map = array();

        $map['is_delete'] = 0;
        $map['id'] = $data['id'];
        $result = Db::table("zstp_dictionary")->field("*")->where($map)->find();
        return json(array("code" => 200, "data" => $result, "message" => "获取成功"));

    }


    public function getthlxById()
    {

        $data = request()->put();
        $map['id'] = $data['id'];
        $result = Db::table('zstp_conf')->where($map)->find();

        $data = explode(",", $result['value']);

        $result['mbnr'] = $data[0];
        $result['thw'] = $data[1];

        return json(array("code" => 200, "data" => $result, "message" => "获取成功"));

    }

    public function delthlxById()
    {
        $data = request()->put();
        $map['id'] = $data['id'];
        $data['is_delete'] = 1;
        $result = Db::table('zstp_conf')->where($map)->update($data);

        $id = $data['id'];

        // 日志
        $sysLogService = new SysLogService();
        $result =  $sysLogService->addLog('删除替换字典('.$id.')', $id, 3, '字典id:'.$id);

        if ($result) {
            return json(array("code" => 200, "data" => $result, "message" => "删除成功"));
        } else {
            return json(array("code" => 400, "data" => $result, "message" => "删除失败"));
        }
    }


    public function thlx()
    {

        $data = request()->put();
        $id = $data['id'];
        if (empty($id)) {

            $mbnr = $data ['mbnr'];
            $thw = $data ['thw'];
            $value = "$mbnr,$thw";
            $data = array(
                "value" => $value,
                "type" => 2,
                "create_time" => date("Y-m-d H:i:s"),
                "update_time" => date("Y-m-d H:i:s"),
                "is_delete" => 0,
                "status" => $data['status']
            );
            $id = Db::table("zstp_conf")->insertGetId($data);

            // 日志
            $sysLogService = new SysLogService();
            $result =  $sysLogService->addLog('新增替换字典('.$mbnr.'替换为,'.$thw.')', $id, 1, '字典id:'.$id);

            return json(array("code" => 200, "data" => [], "message" => "保存成功"));
        } else {

            $mbnr = $data ['mbnr'];
            $thw = $data ['thw'];
            $value = "$mbnr,$thw";
            $data = array(
                "value" => $value,
                "type" => 2,
                "update_time" => date("Y-m-d H:i:s"),
                "is_delete" => 0,
                "status" => $data['status']
            );
            $res = Db::table("zstp_conf")->where(array("id" => $id))->update($data);

            // 日志
            $sysLogService = new SysLogService();
            $result =  $sysLogService->addLog('更新替换字典('.$mbnr.'替换为,'.$thw.')', $id, 2, '字典id:'.$id);

            return json(array("code" => 200, "data" => [], "message" => "保存成功"));

        }


    }

    public function indexglobal()
    {

        $data = request()->put();

        $map["is_delete"] = array("=", 0);
        $list = Db::table("zstp_index_sys")->where($map)->select();


        $data = array();
        foreach ($list as $key => $value) {

            $arr = json_decode($value['extract_content'], true);
            if ($value['index_type'] == 4) {

                $data[] = array(
                    "id" => $value['id'],
                    "k1" => $arr['osName'],
                    "k2" => $arr['newName'],
                    "status" => $value['status']
                );
            }
        }

        return json(array(
            "code" => 200,
            "data" => array("resultList" => $data),
//            "totalCount" => 0,
        ));

    }

    public function textadminister()
    {

        $data = request()->put();
        $length = $data['limit'];
        //$page = 2;
        if (empty($data['page'])) {
            $page = 0;
        } else {
            $page = $data['page'] - 1;
        }
        $offset = $page * $length;
        $map = array();

        $name = $data['name'];


        if ($name == 1) {
            $map['is_delete'] = 0;

            $map['type'] = array("=", 2);
            $result = Db::table('zstp_conf')->where($map)->field("*")->limit($offset, $length)->order(array("create_time" => "desc"))->select();

            foreach ($result as $key => $value) {
                $wordArr = explode(",", $value['value']);
                $result[$key]["k1"] = $wordArr[0];
                $result[$key]["k2"] = $wordArr[1];
            }
            $totalCount = Db::table('zstp_conf')->where($map)->count();
            return json(array(
                "code" => 200,
                "data" => array("resultList" => $result),
                "totalCount" => $totalCount,
            ));

        }

    }

    public function getDictionaryList()
    {

        $data = request()->put();
        $length = $data['limit'];
        //$page = 2;
        if (empty($data['page'])) {
            $page = 0;
        } else {
            $page = $data['page'] - 1;
        }
        $offset = $page * $length;
        $map = array();

        $map['is_delete'] = 0;

        if (!empty($data['dictionary_type'])) {
            $map['dictionary_type'] = array("=", $data['dictionary_type']);
        }

        $result = Db::table('zstp_dictionary')->where($map)->field("*")->limit($offset, $length)->order(array("create_time" => "desc"))->select();
        $totalCount = Db::table('zstp_dictionary')->where($map)->count();
        return json(array(
            "code" => 200,
            "data" => array("resultList" => $result),
            "totalCount" => $totalCount,
        ));


    }


    public function delDictionaryById()
    {
        $data = request()->put();
        $map['id'] = $data['id'];
        $data['is_delete'] = 1;
        $result = Db::table('zstp_dictionary')->where($map)->update($data);

        if ($result) {
            return json(array("code" => 200, "data" => $result, "message" => "删除成功"));
        } else {
            return json(array("code" => 400, "data" => $result, "message" => "删除失败"));
        }
    }


    public function saveDictionary()
    {
        $data = request()->put();
        $id = $data['id'];

        if (empty($data['dictionary_type'])) {
            return json(array('code' => 400, 'message' => '请输入字典类型'));
        }
        if (empty($data['dictionary_value'])) {
            return json(array('code' => 400, 'message' => '请输入字典值'));
        }

        if (empty($id)) {
            $data['create_time'] = date("Y-m-d H:i:s");
            $result = Db::table('zstp_dictionary')->insert($data);
        } else {
            $map['id'] = $id;
            $result = Db::table('zstp_dictionary')->where($map)->update($data);
        }

        if ($result) {
            return json(array("code" => 200, "data" => $result, "message" => "保存成功"));
        } else {
            return json(array("code" => 400, "data" => $result, "message" => "保存失败"));
        }

    }

}